package com.tianma315.web.product.controller;


import java.util.Arrays;
import java.util.Calendar;
import java.util.List;


import com.tianma315.core.common.utils.Result;
import com.tianma315.core.product.domain.ProductDO;
import com.tianma315.core.product.service.ProductService;
import com.tianma315.core.product.vo.ProductVO;
import com.tianma315.web.common.base.BaseController;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;


/**
 * 
 * <pre>
 * 产品
 * </pre>
 * <small> 2019-08-06 14:41:19 | Wen</small>
 */
@Controller
@RequestMapping("/product/product")
public class ProductController extends BaseController {
	@Autowired
	private ProductService productService;
	
	@GetMapping()
	@RequiresPermissions("product:product:product")
	String Product(){
	    return "product/product/product";
	}
	
	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("product:product:product")
	public Result<Page<ProductDO>> list(Integer pageNumber, Integer pageSize, ProductDO productDTO){
		// 查询列表数据
//		productDTO.setMerchantId(getMerchantId());
        Page<ProductDO> page = productService.getProductPage(pageNumber,pageSize,productDTO);
        return Result.ok(page);
	}
	
	@GetMapping("/add")
	@RequiresPermissions("product:product:add")
	String add(){
	    return "product/product/add";
	}

	@GetMapping("/edit/{productId}")
	@RequiresPermissions("product:product:edit")
	String edit(@PathVariable("productId") Integer productId,Model model){
		ProductDO product = productService.getProductById(productId);
		model.addAttribute("product", product);
	    return "product/product/edit";
	}
	
	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("product:product:add")
	public Result<String> save( ProductVO product){
		product.setCreateDate(Calendar.getInstance().getTime());
//		product.setMerchantId(getMerchantId());
		if(productService.addProduct(product)){
			return Result.ok();
		}
		return Result.fail();
	}
	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("product:product:edit")
	public Result<String>  update( ProductVO product){
		productService.updateProduct(product);
		return Result.ok();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("product:product:remove")
	public Result<String>  remove( Integer productId){
		if(productService.deleteProduct(productId)){
			return Result.ok();
		}
		return Result.fail();
	}

	/**
	 * 删除
	 */
	@PostMapping( "/status")
	@ResponseBody
	public Result<String>  status( Integer productId,Integer status){
		if(productService.updateStatus(productId,status)){
			return Result.ok();
		}
		return Result.fail();
	}
	
	/**
	 * 删除
	 */
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("product:product:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Integer[] productIds){
		productService.deleteBatchIds(Arrays.asList(productIds));
		return Result.ok();
	}

	@GetMapping("/all")
	@ResponseBody
	public Result<List<ProductDO>> all(){
		List<ProductDO> productDOS = productService.getProductDOList();
//		for (ProductDO productDO : productDOS){
//			System.out.println("###"+productDO);
//		}
		return Result.ok(productDOS);
	}
}
